package com.hzmw.beetldemo.util;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Hzmw
 * @create 2019-01-18 14:48
 * @email mengwei.zhao@nvrchina.com.cn
 * @description
 */

public class GeneratorUtils {

    public static final String UNDERLINE = "_";
    private final static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static String jdbcToJavaType(String jdbc) {
        jdbc = jdbc.toLowerCase();
        if (jdbc.contains("character") || jdbc.contains("text")) {
            return "String";
        } else if (jdbc.contains("date") || jdbc.contains("timestamp")) {
            return "Date";
        } else if (jdbc.contains("bigint")) {
            return "Long";
        } else if (jdbc.contains("integer") || jdbc.contains("int")) {
            return "Integer";
        }
        jdbc = jdbc.replaceAll("\\(.*\\)", "");
        return jdbc;
    }

    /**
     * 将下划线方式命名的字符串转换为驼峰式
     * 例如：hello_world->HelloWorld
     *
     * @param name 转换前的带下划线的字符串
     * @return 转换后的驼峰式命名的字符串
     */
    public static String toCamelCase(String name) {
        if (name == null) {
            return null;
        }
        if (name.contains(UNDERLINE)) {
            name = name.toLowerCase();

            StringBuilder sb = new StringBuilder(name.length());
            boolean upperCase = false;
            for (int i = 0; i < name.length(); i++) {
                char c = name.charAt(i);

                if (c == '_') {
                    upperCase = true;
                } else if (upperCase) {
                    sb.append(Character.toUpperCase(c));
                    upperCase = false;
                } else {
                    sb.append(c);
                }
            }
            return sb.toString();
        } else {
            return name;
        }
    }

    /**
     * 首字母大写
     */
    public static String firstLetterToUpper(String str) {
        char firstChar = str.charAt(0);
        if (firstChar >= 'a' && firstChar <= 'z') {
            char[] arr = str.toCharArray();
            arr[0] -= ('a' - 'A');
            return new String(arr);
        }
        return str;
    }

    /**
     * 首字母小写
     */
    public static String firstLetterToLower(String str) {
        char firstChar = str.charAt(0);
        if (firstChar >= 'A' && firstChar <= 'Z') {
            char[] arr = str.toCharArray();
            arr[0] += ('a' - 'A');
            return new String(arr);
        }
        return str;
    }

    /**
     * 当前时间
     */
    public static String currentTime() {
        return sdfTime.format(new Date());
    }
}
